import 'reflect-metadata';
import { ContainerConfig } from './di/container';
import { WebSocketServer } from './core/server';
import { LoggerToken } from './domain/interfaces/ILogger';
import type { ILogger } from './domain/interfaces/ILogger';
import { container } from './di/container';

// 配置依赖注入
ContainerConfig.configure();

const logger = container.resolve<ILogger>(LoggerToken);

process.on('uncaughtException', (error) => {
  logger.error('Uncaught Exception', error as Error);
  process.exit(1);
});

process.on('unhandledRejection', (reason) => {
  logger.error('Unhandled Rejection', reason as Error);
  process.exit(1);
});

// 启动服务器
const server = new WebSocketServer();
server.start(parseInt(process.env.PORT || '3000'));

// 优雅关闭
const shutdown = async (signal: string) => {
  logger.info(`Received ${signal}, shutting down gracefully`);
  server.stop();
  process.exit(0);
};

process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
